﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SCG.SolarVengeanceEngine;
using SCG.WinForms;

namespace SCG.SolarVengeance
{
    public partial class StarSystemPanel : BasePanel
    {
        //constructors
        public StarSystemPanel()
            : base()
        {
            InitializeComponent();
        }

        //ensure proper sizing
        public override void Initialize()
        {
            elastic.ForceResize();
        }

        //update for new impulse
        public override void ImpulseUpdate()
        {
            StarSystem sys = GamePage.SystemSelected;
            if (sys == null)
                return;

            //name
            lblName.Text = sys.Name;
            lblName.ForeColor = sys.Color.ToColor();

            //owner
            if (sys.VisibleToCurrentPlayer)
            {
                if (sys.Owner == null)
                {
                    lblOwner.Text = "Independent";
                    picGlyph.Image = null;
                }
                else
                {
                    lblOwner.Text = "Owner: " + sys.Owner.Name;
                    picGlyph.Image = (Image)sys.Owner.Glyph;
                }
            }
            else
            {
                lblOwner.Text = "Out of Scanner Range";
                picGlyph.Image = null;
                ShowControls(elastic, 1, false);
            }
            lblOwner.ForeColor = lblName.ForeColor;

            //properties
            if (sys.VisibleToCurrentPlayer)
            {
                ShowControls(elastic, 1, true);
                lblSysValue.Text = sys.Value.ToString();
                lblSysResources.Text = sys.ResourcesInt.ToString();
                lblSysShields.Text = sys.Shields.ToString();
                lblSysScanners.Text = sys.Scanners.ToString();

                //bonus
                if (sys.BonusValue > 0)
                    lblBonus.Text = sys.BonusValue.ToString("N0") + "% " + sys.BonusShipType + " Bonus";
                else
                    lblBonus.Text = "";

                //command interface
                if (sys.OwnedByCurrentPlayer)
                {
                    btnBuildShield.Enabled = sys.ResourcesInt >= sys.Shields;
                    btnBuildScanners.Enabled = sys.ResourcesInt >= sys.Scanners;
                    btnBuildStarShip.Enabled = sys.ResourcesInt >= 1;
                    _settingStates = true;
                    btnAutoShields.Down = sys.AutoShields;
                    btnAutoScanners.Down = sys.AutoScanners;
                    btnAutoStarShip.Down = sys.AutoStarShip;
                    _settingStates = false;
                    lblAutoShipType.Text = sys.AutoStarShipType.Description;
                }
                else
                    ShowControls(elastic, 2, false);                
            }

            Refresh();
        }

        //build shield
        private void btnBuildShield_Click(object sender, EventArgs e)
        {
            GamePage.SubmitOrder(OrderType.BuildShield, GamePage.SystemSelected);
        }

        //build scanners
        private void btnBuildScanners_Click(object sender, EventArgs e)
        {
            GamePage.SubmitOrder(OrderType.BuildScanner, GamePage.SystemSelected);
        }

        //auto shields
        private bool _settingStates = false;
        private void btnAutoShields_Click(object sender, EventArgs e)
        {
            if (_settingStates)
                return;
            GamePage.SubmitOrder(OrderType.AutoShields, GamePage.SystemSelected, btnAutoShields.Down);
            if (btnAutoShields.Down)
                MainForm.PlaySound("CheckOn");
            else
                MainForm.PlaySound("CheckOff");
        }

        //auto scanners
        private void btnAutoScanners_Click(object sender, EventArgs e)
        {
            if (_settingStates)
                return;
            GamePage.SubmitOrder(OrderType.AutoScanners, GamePage.SystemSelected, btnAutoScanners.Down);
            if (btnAutoScanners.Down)
                MainForm.PlaySound("CheckOn");
            else
                MainForm.PlaySound("CheckOff");
        }

        //auto starship
        private void btnAutoStarShip_Click(object sender, EventArgs e)
        {
            if (_settingStates)
                return;
            GamePage.SubmitOrder(OrderType.AutoStarShip, GamePage.SystemSelected, btnAutoStarShip.Down);
            if (btnAutoStarShip.Down)
                MainForm.PlaySound("CheckOn");
            else
                MainForm.PlaySound("CheckOff");
        }

        //build starship
        private void btnBuildStarShip_Click(object sender, EventArgs e)
        {
            GamePage.SidePanelMode = SidePanels.Build;
        }

        //set auto starship type
        private void lblAutoShipType_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            GamePage.SidePanelMode = SidePanels.SetAutoBuildStarShip;
        }
    }
}
